package com.sinosoft.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	
	public int findPatternSize(String pattern) {
		return redisTemplate.keys(pattern).size();
	}
	
	public Map<String,Object> findPattern(String pattern){
		Set<String> keys = redisTemplate.keys(pattern);
		Map<String,Object> hashMap=new HashMap<String,Object>();
		if (keys.size() > 0) {
			for(String key:keys){
				hashMap.put(key, this.get(key));
			}
		}
		return hashMap;
	}

	public boolean remove(String... keys) {
		try {
			for (String key : keys) {
				remove(key);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean removePattern(String pattern) {
		try {
			if (redisTemplate != null) {
				Set<String> keys = redisTemplate.keys(pattern);
				if (keys.size() > 0)
					redisTemplate.delete(keys);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean remove(String key) {
		try {
			if (redisTemplate != null) {
				if (exists(key)) {
					redisTemplate.delete(key);
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		try {
			if (redisTemplate == null)
				return false;
			return redisTemplate.hasKey(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		try {
			if (redisTemplate == null) {
				return null;
			}
			Object result = null;
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			result = operations.get(key);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try {
			if (redisTemplate == null) {
				System.out.println("redisTemplate is null");
				return false;
			}
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			if (!expireTime.equals(0L)) {
				redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, int minutes) {
		boolean result = false;
		try {
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			if (minutes > 0) {
				redisTemplate.expire(key, minutes, TimeUnit.MINUTES);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean expireTime(final String key, int minutes) {
		boolean result = false;
		try {
			if (minutes > 0) {
				redisTemplate.expire(key, minutes, TimeUnit.MINUTES);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public boolean expireTime(final String key, long expireTime) {
		boolean result = false;
		try {
			if (expireTime > 0) {
				redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			}
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
